package com.t3rik.mes.emp.controller;

import java.util.List;
import java.util.Map;

import com.t3rik.mes.emp.domain.EmpPieceSalaryDetails;
import com.t3rik.mes.emp.domain.vo.EmpPieceSalaryDetailsVo;
import jakarta.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.t3rik.common.annotation.Log;
import com.t3rik.common.core.controller.BaseController;
import com.t3rik.common.core.domain.AjaxResult;
import com.t3rik.common.utils.StringUtils;
import com.t3rik.common.enums.BusinessType;
import com.t3rik.mes.emp.service.IEmpPieceSalaryDetailsService;
import com.t3rik.common.utils.poi.ExcelUtil;
import com.t3rik.common.core.page.TableDataInfo;

/**
 * 员工计件工资详情Controller
 *
 * @author ykh
 * @date 2025-02-19
 */
@RestController
@RequestMapping("/hrm/salary_detail")
public class EmpPieceSalaryDetailsController extends BaseController {
    @Autowired
    private IEmpPieceSalaryDetailsService empPieceSalaryDetailsService;

    /**
     * 查询员工计件工资详情列表
     */
    @PreAuthorize("@ss.hasPermi('hrm:salary_detail:list')")
    @GetMapping("/list")
    public TableDataInfo list(EmpPieceSalaryDetails empPieceSalaryDetails) {
        // 获取查询条件
        LambdaQueryWrapper<EmpPieceSalaryDetails> queryWrapper = getQueryWrapper(empPieceSalaryDetails);
        // 组装分页
        Page<EmpPieceSalaryDetails> page = getMPPage(empPieceSalaryDetails);
        // 查询
        this.empPieceSalaryDetailsService.page(page, queryWrapper);
        List<EmpPieceSalaryDetailsVo> result = empPieceSalaryDetailsService.computeEmpSalary(page.getRecords());
        Page<EmpPieceSalaryDetailsVo> pageVo = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        pageVo.setRecords(result);
        return getDataTableWithPage(pageVo);
    }

    /**
     * 导出员工计件工资详情列表
     */
    @PreAuthorize("@ss.hasPermi('hrm:salary_detail:export')")
    @Log(title = "员工计件工资详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EmpPieceSalaryDetails empPieceSalaryDetails) {
        // 获取查询条件
        LambdaQueryWrapper<EmpPieceSalaryDetails> queryWrapper = getQueryWrapper(empPieceSalaryDetails);
        List<EmpPieceSalaryDetails> list = this.empPieceSalaryDetailsService.list(queryWrapper);
        List<EmpPieceSalaryDetailsVo> result = empPieceSalaryDetailsService.computeEmpSalary(list);
        ExcelUtil<EmpPieceSalaryDetailsVo> util = new ExcelUtil<>(EmpPieceSalaryDetailsVo.class);
        util.exportExcel(response, result, "员工计件工资详情数据");
    }

    /**
     * 获取员工计件工资详情详细信息
     */
    @PreAuthorize("@ss.hasPermi('hrm:salary_detail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(this.empPieceSalaryDetailsService.getById(id));
    }

    /**
     * 新增员工计件工资详情
     */
    @PreAuthorize("@ss.hasPermi('hrm:salary_detail:add')")
    @Log(title = "员工计件工资详情", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EmpPieceSalaryDetails empPieceSalaryDetails) {
        return toAjax(this.empPieceSalaryDetailsService.save(empPieceSalaryDetails));
    }

    /**
     * 修改员工计件工资详情
     */
    @PreAuthorize("@ss.hasPermi('hrm:salary_detail:edit')")
    @Log(title = "员工计件工资详情", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EmpPieceSalaryDetails empPieceSalaryDetails) {
        return toAjax(this.empPieceSalaryDetailsService.updateById(empPieceSalaryDetails));
    }

    /**
     * 删除员工计件工资详情
     */
    @PreAuthorize("@ss.hasPermi('hrm:salary_detail:remove')")
    @Log(title = "员工计件工资详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable List<Long> ids) {
        return toAjax(this.empPieceSalaryDetailsService.removeByIds(ids));
    }

    /**
    * 获取查询条件
    */
    public LambdaQueryWrapper<EmpPieceSalaryDetails> getQueryWrapper(EmpPieceSalaryDetails empPieceSalaryDetails) {
        LambdaQueryWrapper<EmpPieceSalaryDetails> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(empPieceSalaryDetails.getUserName()), EmpPieceSalaryDetails::getUserName, empPieceSalaryDetails.getUserName());
        queryWrapper.eq(empPieceSalaryDetails.getRecordNick() != null, EmpPieceSalaryDetails::getRecordNick, empPieceSalaryDetails.getRecordNick());
        queryWrapper.eq(empPieceSalaryDetails.getPrice() != null, EmpPieceSalaryDetails::getPrice, empPieceSalaryDetails.getPrice());
        queryWrapper.eq(empPieceSalaryDetails.getPercent() != null, EmpPieceSalaryDetails::getPercent, empPieceSalaryDetails.getPercent());
        queryWrapper.eq(empPieceSalaryDetails.getFeedbackCount() != null, EmpPieceSalaryDetails::getFeedbackCount, empPieceSalaryDetails.getFeedbackCount());
        queryWrapper.eq(empPieceSalaryDetails.getCountPrice() != null, EmpPieceSalaryDetails::getCountPrice, empPieceSalaryDetails.getCountPrice());
        queryWrapper.eq(empPieceSalaryDetails.getTotalPrice() != null, EmpPieceSalaryDetails::getTotalPrice, empPieceSalaryDetails.getTotalPrice());
        queryWrapper.eq(empPieceSalaryDetails.getCreateTime() != null, EmpPieceSalaryDetails::getCreateTime, empPieceSalaryDetails.getCreateTime());
        queryWrapper.eq(empPieceSalaryDetails.getUpdateTime() != null, EmpPieceSalaryDetails::getUpdateTime, empPieceSalaryDetails.getUpdateTime());
        queryWrapper.eq(empPieceSalaryDetails.getRemark() != null, EmpPieceSalaryDetails::getRemark, empPieceSalaryDetails.getRemark());
        // 默认创建时间倒序
        queryWrapper.orderByDesc(EmpPieceSalaryDetails::getCreateTime);
        Map<String, Object> params = empPieceSalaryDetails.getParams();
        queryWrapper.between(params.get("beginTime") != null && params.get("endTime") != null,EmpPieceSalaryDetails::getCreateTime, params.get("beginTime"), params.get("endTime"));
        return queryWrapper;
    }
}
